package com.lm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lm.dao.TDmDistrictMapper;
import com.lm.dao.TEntityUniversityMapper;
import com.lm.model.TDmDistrict;
import com.lm.model.TDmDistrictExample;
import com.lm.model.TEntityUniversity;
import com.lm.model.TEntityUniversityExample;
import com.lm.service.UniversityService;
import com.lm.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UniversityServiceImpl implements UniversityService {
    @Autowired
    private TEntityUniversityMapper universityMapper;
    @Autowired
    private TDmDistrictMapper districtMapper;

    @Override
    public Integer addEntity(TEntityUniversity university) {
        String currentUserId = SecurityUitil.getCurrentUserId();
        university.setCreateby(currentUserId);
        university.setEditby(currentUserId);
        university.setId(Guid.get());
        university.setCreatetime(new Date());
        university.setEdittime(new Date());
        university.setStatue("1");
        return universityMapper.insert(university);
    }

    @Override
    public PageInfo getList(String keyword, Integer pageSize, Integer pageNumber) {
        TEntityUniversityExample example = new TEntityUniversityExample();
        TEntityUniversityExample.Criteria criteria = example.createCriteria().andStatueEqualTo("1");
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%"+keyword+"%");
        }
        PageHelper.startPage(pageSize,pageNumber);
        return new PageInfo(universityMapper.selectWithDistricByExample(example));
    }

    @Override
    public Integer updateEntity(TEntityUniversity university) {
        TEntityUniversity tEntityUniversity = universityMapper.selectByPrimaryKey(university.getId());
        university.setEditby(SecurityUitil.getCurrentUserId());
        university.setEdittime(new Date());
        university.setStatue(tEntityUniversity.getStatue());
        university.setCreateby(tEntityUniversity.getCreateby());
        university.setCreatetime(tEntityUniversity.getCreatetime());
        return universityMapper.updateByPrimaryKey(university);
    }

    @Override
    public Integer updateStatue(String id) {
        TEntityUniversity tEntityUniversity = universityMapper.selectByPrimaryKey(id);
        tEntityUniversity.setStatue("0");
        return universityMapper.updateByPrimaryKey(tEntityUniversity);
    }

    @Override
    public List<TEntityUniversity> getListSimple() {
        TEntityUniversityExample example = new TEntityUniversityExample();
        example.createCriteria().andStatueEqualTo("1");
        return universityMapper.selectByExample(example);
    }

    @Override
    public ExcelBaseBean getTemplate() {
        ExcelBaseBean baseBean = new ExcelBaseBean();
        baseBean.setCategory("院校信息");
        baseBean.setManager("计算机系主任");
        baseBean.setCompany("山东科技大学");
        baseBean.setSubject("院校信息表");
        baseBean.setTitle("院校信息");
        baseBean.setAnthor("wn");
        baseBean.setCommnets("无");
        baseBean.setExcelName("院校信息" + Guid.get() + ".xls");
        List<ExcelSheet> excelSheets = new ArrayList<>();
        ExcelSheet sheet = new ExcelSheet();
        sheet.setCreateSheet("院校信息表");
        List<ExcelSheetTitle> sheetTitles = new ArrayList<>();
        ExcelSheetTitle title1 = new ExcelSheetTitle(0,10 * 256,"院校名称");
        ExcelSheetTitle title2 = new ExcelSheetTitle(1,10 * 256,"省份");
        ExcelSheetTitle title3 = new ExcelSheetTitle(2,10 * 256,"地市");
        ExcelSheetTitle title4 = new ExcelSheetTitle(3,10 * 256,"类型(985/211)");
        ExcelSheetTitle title5 = new ExcelSheetTitle(4,10 * 256,"办学(公办/民办)");
        ExcelSheetTitle title6 = new ExcelSheetTitle(5,10 * 256,"院校类型(综合类/师范类)");
        ExcelSheetTitle title7 = new ExcelSheetTitle(6,10 * 256,"隶属(教育部/工信部)");
        ExcelSheetTitle title8 = new ExcelSheetTitle(7,10 * 256,"大学类型(大学/学院)");
        sheetTitles.add(title1);
        sheetTitles.add(title2);
        sheetTitles.add(title3);
        sheetTitles.add(title4);
        sheetTitles.add(title5);
        sheetTitles.add(title6);
        sheetTitles.add(title7);
        sheetTitles.add(title8);
        sheet.setSheetTitles(sheetTitles);
        excelSheets.add(sheet);
        baseBean.setSheets(excelSheets);
        return baseBean;
    }

    @Override
    public int addEntities(List<TEntityUniversity> universities) {
        int suc = 0;
        for (TEntityUniversity university : universities) {
            String currentUserId = SecurityUitil.getCurrentUserId();
            university.setCreateby(currentUserId);
            university.setEditby(currentUserId);
            university.setId(Guid.get());
            university.setCreatetime(new Date());
            university.setEdittime(new Date());
            university.setStatue("1");
            suc +=universityMapper.insert(university);
        }
        return suc;
    }

    @Override
    public List<TDmDistrict> getAllDistrict() {
        TDmDistrictExample example = new TDmDistrictExample();
        example.createCriteria();
        return districtMapper.selectByExample(example);
    }
}
